//
//  DeptSearch.m
//  testProj
//
//  Created by Laurent on 08/07/11.
//  Copyright 2011 Epita. All rights reserved.
//

#import "DeptSearch.h"
#import "Search.h"

@implementation DeptSearch

@synthesize arrayOfDepartments, selectedDepartment;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    /*
    NSString *docPath = [MTIFileManager fullPath:@"departements.plist" In:APP_DOCUMENTS];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError* fileManagerError;
    
    if (![fileManager fileExistsAtPath:docPath]) {
        NSString* ressourceDataPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"departements.plist"];
        if (![fileManager copyItemAtPath:ressourceDataPath
								  toPath:docPath
								   error:&fileManagerError])
			@throw [NSException exceptionWithName:@"FILE_ERROR" reason:@"Imposible to copy blank datas from ressources" userInfo:nil];
    }
    */
    self.arrayOfDepartments = [NSArray arrayWithObjects:@"01 - AIN", @"02 - AISNE", @"03 - ALLIER", @"04 - ALPES DE HAUTE PROVENCE", @"05 - ALPES (HAUTES)", @"06 - ALPES MARITIMES", @"07 - ARDÉCHE", @"08 - ARDENNES", @"09 - ARIÉGE", @"10 - AUBE", @"11 - AUDE", @"12 - AVEYRON", @"13 - BOUCHES DU RHÔNE", @"14 - CALVADOS", @"15 - CANTAL", @"16 - CHARENTE", @"17 - CHARENTE MARITIME", @"18 - CHER", @"19 - CORRÉZE", @"23 - CREUSE ", @"24 - DORDOGNE", @"25 - DOUBS", @"26 - DRÔME", @"27 - EURE", @"28 - EURE ET LOIR", @"29 - FINISTÉRE", @"30 - GARD", @"31 - GARONNE (HAUTE)", @"32 - GERS", @"33 - GIRONDE", @"34 - HÉRAULT", @"35 - ILE ET VILAINE", @"36 - INDRE", @"37 - INDRE ET LOIRE", @"38 - ISÉRE", @"39 - JURA", @"40 - LANDES", @"41 - LOIR ET CHER", @"42 - LOIRE", @"43 - LOIRE (HAUTE)", @"44 - LOIRE ATLANTIQUE", @"45 - LOIRET", @"46 - LOT", @"47 - LOT ET GARONNE", @"48 - LOZÉRE", @"49 - MAINE ET LOIRE", @"50 - MANCHE", @"51 - MARNE", @"52 - MARNE (HAUTE)", @"53 - MAYENNE", @"54 - MEURTHE ET MOSELLE", @"55 - MEUSE", @"56 - MORBIHAN", @"57 - MOSELLE", @"58 - NIÉVRE", @"59 - NORD", @"60 - OISE", @"61 - ORNE", @"62 - PAS DE CALAIS", @"63 - PUY DE DÔME", @"64 - PYRÉNÉES ATLANTIQUES", @"65 - PYRÉNÉES (HAUTES)", @"66 - PYRÉNÉES ORIENTALES", @"67 - RHIN (BAS)", @"68 - RHIN (HAUT)", @"69 - RHÔNE", @"70 - SAÔNE (HAUTE)", @"71 - SAÔNE ET LOIRE", @"72 - SARTHE", @"73 - SAVOIE", @"74 - SAVOIE (HAUTE)", @"75 - PARIS", @"76 - SEINE MARITIME", @"77 - SEINE ET MARNE", @"78 - YVELINES", @"79 - SÈVRES (DEUX)", @"80 - SOMME", @"81 - TARN", @"82 - TARN ET GARONNE", @"83 - VAR", @"84 - VAUCLUSE", @"85 - VENDÉE", @"86 - VIENNE", @"87 - VIENNE (HAUTE)", @"88 - VOSGES", @"89 - YONNE", @"90 - BELFORT (TERRITOIRE DE)", @"91 - ESSONNE", @"92 - HAUTS DE SEINE", @"93 - SEINE SAINT DENIS", @"94 - VAL DE MARNE", @"976 - MAYOTTE", @"971 - GUADELOUPE", @"973 - GUYANE", @"972 - MARTINIQUE", @"974 - RÉUNION", @"21 - CÔTE D'OR", @"22 - CÔTES D'ARMOR", @"95 - VAL D'OISE", nil];
        self.title = @"Recherche avancee";
    //NSLog(@"%i", [arrayOfDepartments count]);
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [arrayOfDepartments count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    cell.textLabel.text = [arrayOfDepartments objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Search *SearchController = [[Search alloc] initWithNibName:@"Search" bundle:nil];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    SearchController.dept = [cell.textLabel.text substringToIndex:2];
    [self.navigationController pushViewController:SearchController animated:YES];
}

@end
